package com.wm.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import util.Escape;

import com.wm.bean.Dish;
import com.wm.bean.Restaurant;
import com.wm.dao.DishDao;
import com.wm.service.MyRestaurantService;
import com.wm.service.RestaurantService;
import com.wm.service.SearchService;
import com.wm.vo.DishListVO;
import com.wm.vo.RestaurantListVO;

import cn.itcast.service.PersonService;
import com.wm.vo.*;
public class SearchAction extends DispatchAction {
	@Resource SearchService searchService;
	private String searchName;
	
	public String getSearchName() {
		return searchName;
	}

	public void setSearchName(String searchName) {
		this.searchName = searchName;
	}

/**
 * 搜索小店
 * @throws UnsupportedEncodingException 
 * 
 */
	
	public ActionForward searchRestaurant(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		
		int page = 1;
		System.out.println(request.getParameter("page"));
		if (request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		RestaurantListVO restaurantListVO = new RestaurantListVO();
		restaurantListVO.requestPage = page;
		restaurantListVO.uid = request.getParameter("uid");
		String keyWord = request.getParameter("keyword");
		restaurantListVO.keyWord  = java.net.URLDecoder.decode(keyWord,"UTF-8");
		request.setAttribute("restaurants", searchService.SearchRest(restaurantListVO));
//		System.out.println(searchService.SearchRest(restaurantListVO).restaurantsList.size());
		return mapping.findForward("restaurantResult");
	}

/**
 * 搜索菜品
 * @throws UnsupportedEncodingException 
 * 
 */
	public ActionForward searchDish(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
		/*int page = 1;
		DishListVO dishListVO = new DishListVO();
		dishListVO.setRequestPage(page) ;*/
		
		int page = 1;
		System.out.println(request.getParameter("page"));
		if (request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page = Integer.parseInt(request.getParameter("page"));
		}
		DishListVO dishListVO = new DishListVO();
		String keyWord = request.getParameter("keyword");
		System.out.println("keyword="+keyWord);
		dishListVO.keyWord  = URLDecoder.decode(request.getParameter("keyword"),"utf-8");
		
		//显示菜名信息
		Dish dish= new Dish();
		
		
		dishListVO.requestPage = page;
		System.out.println("dishListVO.requestPage ="+dishListVO.requestPage);
		String uid = request.getParameter("uid");
		dishListVO.uid = uid;
		request.setAttribute("dishs",searchService.searchDish(dishListVO));
		request.setAttribute("uid",uid);
		//searchService.SearchRest()
		return mapping.findForward("searchDishSuccess");
	}
	/***
	 * 根据主建搜索店铺.
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward searchShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		/*int page = 1;
		DishListVO dishListVO = new DishListVO();
		dishListVO.setRequestPage(page) ;*/
		/*System.out.println(request.getParameter("searchName"));
		//显示菜名信息
		Dish dish= new Dish();
		dish.setName(request.getParameter("searchName"));*/
		//店铺信息
		Restaurant restaurant = new Restaurant();
		restaurant.setRid(request.getParameter("rid"));
		System.out.println(request.getParameter("rid"));
		//List<Restaurant> list = (List<Restaurant>) searchService.searchShop(restaurant);
		request.setAttribute("restaurantList", searchService.searchShop(restaurant));
		/*request.setAttribute("dishList",searchService.searchDish(dish));
		//searchService.SearchRest()
*/		return mapping.findForward("successShop");
	}
	
	/*public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		
		if (request.getParameter("rid").equals("")) {
			return mapping.findForward("notfind");
		}
		String rid = request.getParameter("rid").trim();
		//String rid = "hubei";
		List<Dish> dishList = myRestaurantService.ShowMyRestaurant(rid);
		
		System.out.println(dishList.size());
		for(Dish d:dishList){
			System.out.println(d.getName());
		}
		request.setAttribute("dishs", dishList);
		if (dishList.size()>0) {
			return mapping.findForward("success");
		}else {
			return mapping.findForward("notfind");
		}
		
	}*/

}
